package org.icress.fusion.moc

class TbController {

    static allowedMethods = [save: "POST", update: "POST", delete: "POST"]
    
    def tbService

    def index = {
        redirect(action: "list", params: params)
    }

    def list = {
        params.line = params.line? params.line.trim() : "-"
                
        render view : '/moc/tb/list', model : [tbInstanceList: tbService.list(params), tbInstanceTotal: tbService.count(params)]
    }

    def create = {
        def tbInstance = new Tb()
        tbInstance.properties = params
        render view : '/moc/tb/create', model : [tbInstance: tbInstance]
    }

    def save = {
        def tbInstance = new Tb(params)
        if (tbInstance.validate() && !tbInstance.hasErrors() && tbService.save(tbInstance, params, true)) {
            flash.message = "${message(code: 'default.created.message', args: [message(code: 'moc.tb.label', default: 'Tb'), tbInstance.id])}"
            redirect(action: "show", id: tbInstance.id)
        }else {
            render(view: "/moc/tb/create", model: [tbInstance: tbInstance])
        }
    }

    def show = {
        def tbInstance = tbService.get(params.id)
        if (!tbInstance) {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'moc.tb.label', default: 'Tb'), params.id])}"
            redirect(action: "list")
        }else {
            render view : '/moc/tb/show', model : [tbInstance: tbInstance]
        }
    }

    def edit = {
        def tbInstance = tbService.get(params.id)
        if (!tbInstance) {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'moc.tb.label', default: 'Tb'), params.id])}"
            redirect(action: "list")
        }
        else {
            render view : '/moc/tb/edit',  model : [tbInstance: tbInstance]
        }
    }

    def update = {
        def tbInstance = tbService.get(params.id)
        if (!tbInstance) {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'moc.tb.label', default: 'Tb'), params.id])}"
            redirect(action: "list")
            return
        }
            
        if (params.version) {
            def version = params.version.toLong()
            if (tbInstance.version > version) {
                
                tbInstance.errors.rejectValue("version", "default.optimistic.locking.failure", [message(code: 'moc.tb.label', default: 'Tb')] as Object[], "Another user has updated this Tb while you were editing")
                render(view: "edit", model: [tbInstance: tbInstance])
                return
            }
        }
        
        tbInstance.properties = params
		
        if (tbInstance.validate() && !tbInstance.hasErrors() && tbService.update(tbInstance, params, true)) {
            flash.message = "${message(code: 'default.updated.message', args: [message(code: 'moc.tb.label', default: 'Tb'), tbInstance.id])}"
            redirect(action: "show", id: tbInstance.id)
        }else {
            render(view: "/moc/tb/edit", model: [tbInstance: tbInstance])
        }
    }

    def delete = {
        def tbInstance = tbService.get(params.id)
        if (!tbInstance) {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'moc.tb.label', default: 'Tb'), params.id])}"
            redirect(action: "list")
        }
        try {
            tbService.delete(tbInstance, true)
            flash.message = "${message(code: 'default.deleted.message', args: [message(code: 'moc.tb.label', default: 'Tb'), params.id])}"
            redirect(action: "list")
        }catch (org.springframework.dao.DataIntegrityViolationException e) {
            flash.message = "${message(code: 'default.not.deleted.message', args: [message(code: 'moc.tb.label', default: 'Tb'), params.id])}"
            redirect(action: "show", id: params.id)
        }
    }
}